import { ElMessageBox } from 'element-plus';
import dayjs from '@/utils/my-dayjs'
let myutil = {
    /** 解析ztree node的id值（id值由前缀-记录id构成，比如 cat-101 leaf-1001） */
    parseTreeNodeId(nodeId:string){
        let node = {id:<number>NaN, type:''};
        if(!nodeId) return node;
        let idx = nodeId.indexOf('-')
        if(idx>=0){
            node.id = parseInt(nodeId.substr(idx+1));
            node.type = nodeId.substr(0, idx);
        }else{
            node.id = parseInt(nodeId);
        }
        return node;
    },    
    /** Element Plus Select Options */
    codeItems2ElOptions(codeItems:any){
        if(!codeItems)return [];
        let selectOptions : Array<{value:string, label:string}> = codeItems.map((codeItem: any) => { return {value:codeItem.code, label: codeItem.name} });
        return selectOptions;
    },
    /** Element Plus Select Options */
    nameItems2ElOptions(nameItems:any){
        if(!nameItems)return [];
        let selectOptions : Array<{value:string, label:string}>  = nameItems.map((nameItem: any) => { return {value:nameItem.id, label: nameItem.name } });
        return selectOptions;
    },
    getDateStr(datetimeStr:string|undefined){
        if(!datetimeStr)return '';
        let idx = datetimeStr.indexOf(' ');
        if(idx>0){
            let datestr = datetimeStr.substring(0, idx);
            return datestr;
        }else {
            return datetimeStr;
        }
    },
    getTimeStr(datetimeStr:string|undefined){
        if(!datetimeStr)return '';
        let idx = datetimeStr.indexOf(' ');
        if(idx>0){
            let timestr = datetimeStr.substring(idx+1);
            return timestr;
        }else {
            return '';
        }
    },
    getDateTimeStr(datestr:string|undefined,timestr:string|undefined){
        if(!datestr)return '';
        if(timestr){
            return datestr+" "+timestr;
        }else{
            return datestr;
        }
    },
    confirmForDelete(msg:string){
        return ElMessageBox.confirm(msg, '提示',{
            type:'warning',
            confirmButtonText: '确认',
            cancelButtonText: '取消',    
          }); 
    },
    fmtDateMinute(datetimeStr:any){
        if(!datetimeStr)return '';
        return dayjs(datetimeStr).format('YYYY-MM-DD HH:mm');
    },
    fmtDateTime(datetimeStr:any){
        if(!datetimeStr)return '';
        return dayjs(datetimeStr).format('YYYY-MM-DD HH:mm:ss');
    }
}

export default myutil